package com.neu.controller;

import com.neu.pojo.Reception;
import com.neu.pojo.RoomType;
import com.neu.pojo.Staff;
import com.neu.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

@Controller
public class StaffController {
    @Autowired
    private StaffService staffService;

    /**
     * 员工登录
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public ModelAndView login(Staff staff){
        ModelAndView mv=new ModelAndView();
        Staff s = staffService.login(staff);
       if(s!=null){
           String staffType=s.getStaffType();
           if(staffType.equals("Reception")){
               mv.addObject("code",0);
               mv.addObject("staff",s);
               mv.setViewName("menu");
           }
           else {
               mv.addObject("code",0);
               mv.addObject("staff",s);
               mv.setViewName("menu2");
           }

       }
       else {
           mv.addObject("code",-1);
           mv.addObject("staff",staff);
           mv.setViewName("login");
       }
       return mv;
    }

    /**
     * 查询用户列表
     * @param staffvo
     * @return
     */
//    @ResponseBody
//    @RequestMapping("/list")
//    public DataGridViewResult list(Staffvo staffvo, Principal principal){
//
//        //查询当前登录用户
//        Staff loginStaff = staffService.findStaffByStaffName(principal.getName());
//        //如果当前登录用户是普通用户，则只能查询userType为Reception的用户信息
//        if(loginStaff!=null && loginStaff.getStaffType()=="Reception"){
//            staffvo.setStaffType("Reception");
//        }
//
//        //设置分页信息
//        PageHelper.startPage(staffvo.getPage(),staffvo.getLimit());
//        //调用查询的方法
//        List<Staff> staffList = staffService.findStaffList(staffvo);
//        //创建分页对象
//        PageInfo<Staff> pageInfo = new PageInfo<Staff>(staffList);
//        //返回对象
//        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
//    }
    /**
     * 添加用户
     * @param staff
     * @return
     */
    @ResponseBody
    @RequestMapping("addStaff")
    public HashMap addStaff(Staff staff){
        HashMap<String,Object> map = new HashMap<>();
        //获取当前登录用户信息
//        Staff loginStaff = staffService.findStaffByStaffName(principal.getName());
        int res=staffService.insertStaff(staff);
//        if(loginStaff.getStaffType()=="Administrator") {
//            if (staffService.insertStaff(staff) > 0) {
//                map.put("success", true);
//                map.put("message", "添加成功");
//            } else {
//                map.put("success", false);
//                map.put("message", "添加失败");
//            }
//        }
//        else{map.put("success", false);
//            map.put("message", "添加失败");}
////        return JSON.toJSONString(map);
        if(res>0){
            map.put("code",0);
            map.put("msg","添加成功");
        }
        else{
            map.put("code",-1);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * 修改用户
     * @param staff
     * @return
     */
    @ResponseBody
    @RequestMapping("updateStaff")
    public HashMap updateStaff(Staff staff){
        HashMap<String,Object> map = new HashMap<String,Object>();
        //获取当前登录用户信息
//        Staff loginStaff = staffService.findStaffByStaffName(principal.getName());
        int res=staffService.updateStaff(staff);
//        if(loginStaff.getStaffType()=="Administrator") {
//            if(staffService.updateStaff(staff)>0){
//                map.put("success",true);
//                map.put("message","修改成功");
//            }else{
//                map.put("success",false);
//                map.put("message","修改失败");
//            }}
//        else{map.put("success", false);
//            map.put("message","修改失败");}
////        return JSON.toJSONString(map);
        if(res>0){
            map.put("code",0);
            map.put("msg","修改成功");
        }
        else{
            map.put("code",-1);
            map.put("msg","修改失败");
        }
        return map;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteStaffById")
    public HashMap deleteStaffById(int id){
        HashMap<String,Object> map = new HashMap<String,Object>();

//        Staff loginStaff = staffService.findStaffByStaffName(principal.getName());
        int res=staffService.deleteStaffById(id);
//        if(loginStaff.getStaffType()=="Administrator") {
//            if(staffService.deleteStaffById(id)>0){
//                map.put("success",true);
//                map.put("message","删除成功");
//            }else{
//                map.put("success",false);
//                map.put("message","删除失败");
//            }}
//        else{map.put("success", false);
//            map.put("message","修改失败");}
////        return JSON.toJSONString(map);
        if(res>0){
            map.put("code",0);
            map.put("msg","删除成功");
        }
        else{
            map.put("code",-1);
            map.put("msg","删除失败");
        }
        return map;
    }

    @RequestMapping("getStaffByPage")
    @ResponseBody
    public HashMap getStaffByPage(int page,int limit){
        List<Staff> list=staffService.getStaffByPage(page,limit);
        int totalCount = staffService.getStaffCount();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",totalCount);
        map.put("data",list);
        return map;
    }
}
